#include "OptData.h"
#include "LBFGS.h"
#include "LBFGSBopt.h"
#include "ConjuGradient.h"
#include "GeneralSetup.h"
#include "anyoption.h"
#include "MDopt.h"
#include "boost/date_time/posix_time/posix_time.hpp"

using namespace std;
using namespace boost::posix_time;

int main(int argc, char* argv[]) {
	AnyOption opt;
	opt.addUsage("");
	opt.addUsage("Usage: ");
	opt.addUsage("--setupFile                       setupFile file");
	opt.addUsage("--inputDoseFile                   Dose File");
	opt.addUsage("--resolution						dvh resolution");
	opt.setOption("setupFile");
	opt.setOption("inputDoseFile");
	opt.setOption("resolution");
	opt.processCommandArgs(argc, argv);
	string setupFile = "";
	string inputDoseFile="";
	double resolution = 0.01;
	if (opt.getValue("setupFile") != NULL) {
		setupFile = opt.getValue("setupFile");
	} else {
		opt.printUsage();
		return 0;
	}
	if (opt.getValue("inputDoseFile") != NULL) {
		inputDoseFile = opt.getValue("inputDoseFile");
	} else {
		opt.printUsage();
		return 0;
	}
	if (opt.getValue("resolution") != NULL) {
		resolution = atof(opt.getValue("resolution"));
	}
	cout<<"resolution = "<<resolution<<endl;
	
	GeneralSetup setup;
	setup.parseSetupFile(setupFile);

	OptData optData;
	optData.inputDoseSize(setup.dosegridFile);   // input the size of dose matrix

	//for(int indexField=0;indexField<setup.fieldFiles.size();indexField++){
	//	optData.inputField(indexField,setup.fieldFiles[indexField].dijFile,setup.fieldFiles[indexField].weightFile);
	//}

	for(int indexStruct=0;indexStruct<setup.structureFiles.size();indexStruct++){
		optData.inputStructure(setup.structureFiles[indexStruct].name,setup.structureFiles[indexStruct].structureFile);
	}

	//vector<SConstraint> vConstrains; // add constrains 
	//vConstrains = setup.objectives;

	//vector<float> objFunValue;

	//ptime start = boost::posix_time::microsec_clock::local_time();

	//optimization
	//vector<float> resultVWeight;

	

	//output result data
	
	vector<float> resultDose;
	resultDose = optData.inputDose(inputDoseFile);
	optData.outputDVH(&resultDose, setup.outputDir,resolution);

	return 0;
}